{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1f25a7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.instrument import FXOption\n",
    "from gs_quant.risk import MarketDataShockBasedScenario, MarketDataPattern, MarketDataShock, MarketDataShockType\n",
    "from gs_quant.risk import FXSpot, Price, FXAnnualImpliedVol, FXAnnualATMImpliedVol\n",
    "from gs_quant.session import GsSession\n",
    "\n",
    "GsSession.use(client_id=None, client_secret=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5efa07c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "fxoption = FXOption(pair='EURUSD', expiration_date='3m', premium=0)\n",
    "unshifted_res = fxoption.calc((FXSpot, Price, FXAnnualImpliedVol, FXAnnualATMImpliedVol))\n",
    "unshifted_res.to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a62b8561",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set FX Spot to X\n",
    "\n",
    "x = 1.1\n",
    "\n",
    "mkt_shock = MarketDataShockBasedScenario(\n",
    "    shocks={MarketDataPattern('FX', 'USD/EUR'): MarketDataShock(MarketDataShockType.Override, x)}\n",
    ")\n",
    "\n",
    "with mkt_shock:\n",
    "    shifted_spot = fxoption.calc(FXSpot)\n",
    "shifted_spot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b55ee1cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Bump FX spot by x\n",
    "\n",
    "# relative - 5%\n",
    "x = 0.05\n",
    "\n",
    "mkt_shock = MarketDataShockBasedScenario(\n",
    "    shocks={MarketDataPattern('FX', 'USD/EUR'): MarketDataShock(MarketDataShockType.Proportional, x)}\n",
    ")\n",
    "\n",
    "with mkt_shock:\n",
    "    shifted_spot = fxoption.calc(FXSpot)\n",
    "print(shifted_spot)\n",
    "print(unshifted_res[FXSpot] * (1 + x))\n",
    "\n",
    "# absolute - 0.08\n",
    "x = 0.08\n",
    "\n",
    "mkt_shock = MarketDataShockBasedScenario(\n",
    "    shocks={MarketDataPattern('FX', 'USD/EUR'): MarketDataShock(MarketDataShockType.Absolute, x)}\n",
    ")\n",
    "\n",
    "with mkt_shock:\n",
    "    shifted_spot = fxoption.calc(FXSpot)\n",
    "print(shifted_spot)\n",
    "print(unshifted_res[FXSpot] + x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ffacf30",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set Strike Vol to x  & Bump Strike Vol to x\n",
    "\n",
    "x = 0.06\n",
    "\n",
    "mkt_shock = MarketDataShockBasedScenario(\n",
    "    shocks={\n",
    "        MarketDataPattern('FX VOL', 'USD/EUR', 'ATM VOL'): MarketDataShock(MarketDataShockType.Override, x),\n",
    "        MarketDataPattern('FX VOL', 'USD/EUR', 'BF 25'): MarketDataShock(MarketDataShockType.Override, 0),\n",
    "        MarketDataPattern('FX VOL', 'USD/EUR', 'RR 25'): MarketDataShock(MarketDataShockType.Override, 0),\n",
    "    }\n",
    ")\n",
    "\n",
    "with mkt_shock:\n",
    "    shifted_vol = fxoption.calc(FXAnnualImpliedVol)\n",
    "print(shifted_vol)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6defbc3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Parallel Shift the ATMVol Curve by x\n",
    "\n",
    "# relative - 5%\n",
    "x = 0.05\n",
    "\n",
    "mkt_shock = MarketDataShockBasedScenario(\n",
    "    shocks={MarketDataPattern('FX VOL', 'USD/EUR', 'ATM VOL'): MarketDataShock(MarketDataShockType.Proportional, x)}\n",
    ")\n",
    "\n",
    "with mkt_shock:\n",
    "    shifted_vol = fxoption.calc(FXAnnualATMImpliedVol)\n",
    "print(shifted_vol)\n",
    "print(unshifted_res[FXAnnualATMImpliedVol] * (1 + x))\n",
    "\n",
    "# absolute - 0.01\n",
    "x = 0.01\n",
    "\n",
    "mkt_shock = MarketDataShockBasedScenario(\n",
    "    shocks={MarketDataPattern('FX VOL', 'USD/EUR', 'ATM VOL'): MarketDataShock(MarketDataShockType.Absolute, x)}\n",
    ")\n",
    "\n",
    "with mkt_shock:\n",
    "    shifted_vol = fxoption.calc(FXAnnualATMImpliedVol)\n",
    "print(shifted_vol)\n",
    "print(unshifted_res[FXAnnualATMImpliedVol] + x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecaf3874",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set atmvol at expiry X to y\n",
    "\n",
    "x = '3m'\n",
    "y = 0.62\n",
    "\n",
    "mkt_shock = MarketDataShockBasedScenario(\n",
    "    shocks={\n",
    "        MarketDataPattern('FX VOL', 'USD/EUR', 'ATM VOL', mkt_point=[x]): MarketDataShock(\n",
    "            MarketDataShockType.Override, y\n",
    "        )\n",
    "    }\n",
    ")\n",
    "\n",
    "with mkt_shock:\n",
    "    shifted_atmvol = fxoption.calc(FXAnnualATMImpliedVol)\n",
    "shifted_atmvol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d53e3f2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Parallel Shift the ATMVol at expiry x by y\n",
    "\n",
    "x = '3m'\n",
    "# relative - 5%\n",
    "y = 0.05\n",
    "\n",
    "mkt_shock = MarketDataShockBasedScenario(\n",
    "    shocks={\n",
    "        MarketDataPattern('FX VOL', 'USD/EUR', 'ATM VOL', mkt_point=[x]): MarketDataShock(\n",
    "            MarketDataShockType.Proportional, y\n",
    "        )\n",
    "    }\n",
    ")\n",
    "\n",
    "with mkt_shock:\n",
    "    shifted_vol = fxoption.calc(FXAnnualATMImpliedVol)\n",
    "print(shifted_vol)\n",
    "print(unshifted_res[FXAnnualATMImpliedVol] * (1 + y))\n",
    "\n",
    "# absolute - 0.01\n",
    "y = 0.01\n",
    "\n",
    "mkt_shock = MarketDataShockBasedScenario(\n",
    "    shocks={\n",
    "        MarketDataPattern('FX VOL', 'USD/EUR', 'ATM VOL', mkt_point=[x]): MarketDataShock(\n",
    "            MarketDataShockType.Absolute, y\n",
    "        )\n",
    "    }\n",
    ")\n",
    "\n",
    "with mkt_shock:\n",
    "    shifted_vol = fxoption.calc(FXAnnualATMImpliedVol)\n",
    "print(shifted_vol)\n",
    "print(unshifted_res[FXAnnualATMImpliedVol] + y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c14d137",
   "metadata": {},
   "outputs": [],
   "source": [
    "# parallel shift the yield curve\n",
    "\n",
    "ir_spot_scenario = MarketDataShockBasedScenario(\n",
    "    shocks={MarketDataPattern('IR', 'EUR'): MarketDataShock(MarketDataShockType.Absolute, 50 / 10000)}\n",
    ")\n",
    "\n",
    "with ir_spot_scenario:\n",
    "    yield_curve_shift = fxoption.price()\n",
    "\n",
    "print(yield_curve_shift)\n",
    "print(unshifted_res[Price])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b93b3651",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
